// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: Unified Transport Abstraction Layer Unified Transport Abstraction Layer
// ------------------------------------------------------------------

#include <TCPChannelResource.h>

#include <chrono>
#include <thread>

#include <ertps/utils/LocatorAlias.h>
#include <TCPTransportInterface.h>

namespace vbs {
namespace transport {

using IPLocator = ertps::rtps::IPLocator;

TCPChannelResource::TCPChannelResource(TCPTransportInterface* parent, const Locator& locator,
                                       const Locator& remote_locator, uint32_t maxMsgSize)
    : ChannelResource(maxMsgSize),
      parent_(parent),
      locator_(locator),
      remote_locator_(remote_locator),
      connection_status_(eConnectionStatus::eDisconnected),
      tcp_connection_type_(TCPConnectionType::TCP_CONNECT_TYPE) {}
TCPChannelResource::TCPChannelResource(TCPTransportInterface* parent, const Locator& locator, uint32_t maxMsgSize)
    : ChannelResource(maxMsgSize),
      parent_(parent),
      locator_(locator),
      connection_status_(eConnectionStatus::eDisconnected),
      tcp_connection_type_(TCPConnectionType::TCP_CONNECT_TYPE) {}

TCPChannelResource::TCPChannelResource(TCPTransportInterface* parent, uint32_t maxMsgSize)
    : ChannelResource(maxMsgSize),
      parent_(parent),
      locator_(),
      connection_status_(eConnectionStatus::eConnected),
      tcp_connection_type_(TCPConnectionType::TCP_ACCEPT_TYPE) {}

void TCPChannelResource::disable() {
    ChannelResource::disable();  // prevent asio callback workings on this channel.

    disconnect();
}

}  // namespace transport
}  // namespace vbs
